<?php
	include("../connect/connect.php");
	// เพิ่มข้อมูลใน event
	$sql = "select * from rev_opmhelp where sendto_opmhelp <> 1";
	$rs=mysql_query($sql);
	//echo "<br>".$sql;
	while($row = mysql_fetch_array($rs)){//วนลูปเพิ่มเหตุการณ์จาก event_daily ไปที่ dscc.event
		//get pk  --> evnid
		$pid=$row["pid"];
		$sql = "select pid from opmhelp where pid='$pid'";
		//echo "<br>".$sql;
		$rs2=mysql_query($sql);
		$pid=$row["pid"];
		$cause=$row["cause"];
		$rev_first_date=$row["rev_first_date"];
		$rev_first_amount=$row["rev_first_amount"];
		$per_rev_first=$row["per_rev_first"];
		$rev_all_date=$row["rev_all_date"];
		$rev_all_amount=$row["rev_all_amount"];
		$per_rev_all=$row["per_rev_all"];
		//echo "<br>rs2=".$rs2;
		if($row2=mysql_fetch_array($rs2)){//update
			$sql="update opmhelp set cause='$cause', rev_first_date='$rev_first_date', rev_first_amount='$rev_first_amount', per_rev_first='$per_rev_first', rev_all_date='$rev_all_date', rev_all_amount='$rev_all_amount', per_rev_all='$per_rev_all' where pid='$pid'";
		}else{//insert
			$sql="insert into opmhelp(pid, cause, rev_first_date, rev_first_amount, per_rev_first, rev_all_date, rev_all_amount, per_rev_all) values('$pid','$cause','$rev_first_date','$rev_first_amount','$per_rev_first','$rev_all_date','$rev_all_amount','$per_rev_all')";
		}
		//echo "<br>".$sql;
		$rs2=mysql_query($sql);
		
	}//จบการวนลูปเพิ่มหรือปรับปรุง opmhelp
	
	//ปรับปรุง rev_opmhelp สถานะส่งแล้ว	
	$tempY=date("Y");
	if($tempY<"2550"){
		$tempY=(int)$tempY+543;
	}
	$datetime_send= $tempY.date("-m-d-H:i");
	
	$sql = "update rev_opmhelp set sendto_opmhelp=1,datetime_sendto_opmhelp='$datetime_send'  where sendto_opmhelp=0;";
	$rs=mysql_query($sql);
	mysql_close($c);
	echo "<br><br>complete insert/update opem help";
?>